//
//  VC_CourseList.m
//  yjw-winner
//
//  Created by 隆霁 on 2018/9/25.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_CourseList.h"
#import "Cell_CourseList.h"
#import "Cell_CourseList2.h"

static NSString *identifier = @"course_list_cell_identifer";
static NSString *identifier2 = @"course_list_cell_identifer2";
@interface VC_CourseList ()<UITableViewDelegate, UITableViewDataSource> {
    UITableView *_tableView;
    UIScrollView *_headView;
}
@property (nonatomic, strong) NSArray *headArray;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *buttons;
@property (nonatomic, copy) NSString *sectionTitle;
@end

@implementation VC_CourseList

- (NSArray *)headArray {
    if (!_headArray) {
        _headArray = @[@"2018年上半年",
                       @"2017年下半年",
                       @"2017年上半年",
                       @"2016年下半年",
                       @"2016年上半年"];
    }
    return _headArray;
}
- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@[@"1",
                         @"2"],
                       @[@"1",
                         @"2",
                         @"3",
                         @"4"]];
    }
    return _dataArray;
}
- (NSString *)sectionTitle {
    if (!_sectionTitle) {
        _sectionTitle = self.headArray[0];
    }
    return _sectionTitle;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MJRefreshColor(236, 236, 236);
    [self initHeadView];
    [self initTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 初始化headView
 */
- (void)initHeadView {
    CGFloat w = 105;
    CGFloat h = 45;
    _headView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
    _headView.showsVerticalScrollIndicator = NO;
    _headView.showsHorizontalScrollIndicator = NO;
    _headView.contentSize = CGSizeMake(w * self.headArray.count, h);
    _headView.backgroundColor = MJRefreshColor(50, 50, 50);
    [self.view addSubview:_headView];
    _headView.bounces = NO;
    NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:self.headArray.count];
    for (int i = 0; i < self.headArray.count; i++) {
        NSString *title = self.headArray[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:MAIN_COLOR forState:UIControlStateSelected];
        button.frame = CGRectMake(w * i, 0, w, h);
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = 1000 + i;
        [_headView addSubview:button];
        [mArr addObject:button];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            button.selected = YES;
        }
    }
    self.buttons = [NSArray arrayWithArray:mArr];
}
/**
 初始化tableView
 */
- (void)initTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.tableHeaderView = _headView;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.sectionFooterHeight = 0.0f;
    _tableView.sectionHeaderHeight = 30.0f;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = self.view.backgroundColor;
}


#pragma mark --mark UITableViewDelegate UITableViewDataSourse

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        Cell_CourseList *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[Cell_CourseList alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        return cell;
    }else {
        Cell_CourseList2 *cell = [tableView dequeueReusableCellWithIdentifier:identifier2];
        if (!cell) {
            cell = [[Cell_CourseList2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier2];
        }
        return cell;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *arr = self.dataArray[section];
    return arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0f;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.sectionTitle;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [MBProgressHUD showSuccess:[NSString stringWithFormat:@"did selected %lu, %lu", indexPath.section, indexPath.row]];
}
#pragma mark --mark EVENT

- (void)buttonClick:(UIButton *)sender {
    if (sender.isSelected) {
        return;
    }
    for (UIButton *button in self.buttons) {
        button.selected = NO;
    }
    sender.selected = YES;
    self.sectionTitle = self.headArray[sender.tag - 1000];
    [_tableView reloadData];
}
@end
